/************************************************************************************** 
    WFProcessManager

    Copyright (C) 2010 =OsDevD=

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 2.1 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 **************************************************************************************
 */
package ez.workflow.process;

import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

import ez.workflow.WFEngine;
import ez.workflow.task.WFTaskHandleQueue;

/**
 * 
 * @author OsDevD
 * 
 *         WFProcessManager is a Manager implementation for WFProcess (s) based
 *         on queue design The primary goal of Manager is to trigger running
 *         process in a queue and make sure that running processes are available
 *         for tasks execution
 * 
 */
public class WFProcessManager {
	


	// TODO
	// List/Queue/LinkedList/Collection processManager
	private final Set processSet;
	private final WFTaskHandleQueue taskHandleQueue = new WFTaskHandleQueue();
	private WFEngine engine = null;
	private int next;

	/**
	 * default WFProcessManager constructor 
	 */
	public WFProcessManager() {

		processSet = new HashSet();
	}

	/**
	 * start process / mark start/ assign wfId
	 * @param process
	 */
	public void start(WFProcess process) {
		processSet.add(process);
		process.setProcessManager(this);
		process.setWfid(nextId());
		process.processStart();
	}

	/**
	 * add process to the ProcessManager /can be moved to delegate
	 * @param process
	 */
	public synchronized void add(WFProcess process) {
		if (process.getProcessManager() == null) {
			process.setProcessManager(this);
		}
		if (process.getWfid() == 0) {
			process.setWfid(nextId());
		}
		processSet.add(process);

	}
	
	
	/**
	 * 
	 */
	private synchronized int nextId() {
		int current = next;
		next++;
		return current;
	}
	
	/**
	 * 
	 * @return
	 */
	public synchronized WFTaskHandleQueue getTaskHandleQueue() {
		return taskHandleQueue;
	}
	
	/**
	 * 
	 * @param engine
	 * @param properties
	 */
	public void initialize(WFEngine engine, Properties properties) {
		
	}
	


}
